package com.xfdmao.fcat.coin.huobi.globle.api;

import com.xfdmao.fcat.coin.huobi.globle.request.CreateOrderRequest;
import com.xfdmao.fcat.coin.huobi.globle.request.DepthRequest;
import com.xfdmao.fcat.coin.huobi.globle.request.IntrustOrdersDetailRequest;
import com.xfdmao.fcat.coin.huobi.globle.response.*;
import com.xfdmao.fcat.coin.huobi.globle.response.KlineResponse;
import com.xfdmao.fcat.coin.huobi.globle.response.MergedResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    static final String API_KEY = "";
    static final String API_SECRET = "";

    public static void main(String[] args) {
        try {
            // create ApiClient using your api key and api secret:
            ApiClient client = new ApiClient(API_KEY, API_SECRET);

            TradeResponse trade = client.trade("ethusdt");
            print(trade);
        } catch (ApiException e) {
            System.err.println("API Error! err-code: " + e.getErrCode() + ", err-msg: " + e.getMessage());
            e.printStackTrace();
        }
    }
    public static void test() {
        try {
            apiSample();
        } catch (ApiException e) {
            System.err.println("API Error! err-code: " + e.getErrCode() + ", err-msg: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    static void apiSample() {
        // create ApiClient using your api key and api secret:
        ApiClient client = new ApiClient(API_KEY, API_SECRET);
        // get symbol list:
        print(client.getSymbols());

        //获取 K 线
        //------------------------------------------------------ kline -------------------------------------------------------
        //{"status":"ok","ch":"market.btcusdt.kline.15min","ts":"1564367445645","err-code":null,"err-msg":null,"data":[{"id":1564367400,"amount":6.801922,"count":122,"open":9644.0,"close":9641.0,"low":9640.0,"high":9644.0,"vol":65585.16512045},{"id":1564366500,"amount":144.58496250184476,"count":1260,"open":9652.97,"close":9644.0,"low":9642.0,"high":9660.0,"vol":1395046.5064259286}]}
        KlineResponse kline = client.kline("btcusdt", "15min", "2");
        print(kline);

        //------------------------------------------------------ merged -------------------------------------------------------
        //{"status":"ok","ch":"market.btcusdt.detail.merged","ts":1564367784247,"err-code":null,"err-msg":null,"data":null}
        MergedResponse merged = client.merged("ethusdt");
        print(merged);

        //------------------------------------------------------ depth -------------------------------------------------------
//{"status":"ok","ch":"market.btcusdt.depth.step0","ts":"1564367874221","err-code":null,"err-msg":null,"tick":{"id":null,"ts":"1564367873014","bids":[[9632.420000000000000000,0.017400000000000000],[9631.930000000000000000,0.001000000000000000],[9631.400000000000000000,0.001000000000000000],[9631.010000000000000000,0.570615000000000000],[9630.740000000000000000,0.010000000000000000],[9630.730000000000000000,0.010000000000000000],[9629.930000000000000000,0.100000000000000000],[9629.320000000000000000,0.200000000000000000],[9628.980000000000000000,0.001000000000000000],[9627.810000000000000000,0.102830000000000000],[9627.290000000000000000,0.115000000000000000],[9626.520000000000000000,1.868305000000000000],[9626.440000000000000000,0.892200000000000000],[9626.430000000000000000,0.100000000000000000],[9626.280000000000000000,2.000000000000000000],[9626.270000000000000000,0.084297000000000000],[9626.220000000000000000,0.004100000000000000],[9626.170000000000000000,0.006000000000000000],[9626.090000000000000000,0.200005000000000000],[9626.080000000000000000,0.050000000000000000],[9626.020000000000000000,0.207661000000000000],[9626.000000000000000000,0.200000000000000000],[9625.770000000000000000,0.056000000000000000],[9625.670000000000000000,0.400041000000000000],[9625.550000000000000000,0.000300000000000000],[9625.000000000000000000,0.007460000000000000],[9624.650000000000000000,0.004300000000000000],[9624.040000000000000000,0.230000000000000000],[9623.730000000000000000,3.576862000000000000],[9623.330000000000000000,0.054163000000000000],[9622.960000000000000000,1.163100000000000000],[9622.880000000000000000,0.014866000000000000],[9622.840000000000000000,0.311461000000000000],[9622.820000000000000000,0.001679000000000000],[9622.720000000000000000,0.000300000000000000],[9622.470000000000000000,0.000400000000000000],[9622.440000000000000000,0.000138000000000000],[9622.400000000000000000,0.005000000000000000],[9622.260000000000000000,0.001169000000000000],[9622.190000000000000000,0.108329000000000000],[9622.070000000000000000,1.400000000000000000],[9622.000000000000000000,0.165371000000000000],[9621.740000000000000000,0.000312000000000000],[9621.710000000000000000,0.016178000000000000],[9621.530000000000000000,0.000533000000000000],[9621.400000000000000000,0.391900000000000000],[9621.380000000000000000,0.162456000000000000],[9621.310000000000000000,0.004900000000000000],[9621.070000000000000000,1.500000000000000000],[9620.850000000000000000,0.487436000000000000],[9620.820000000000000000,0.830698000000000000],[9620.760000000000000000,0.934483000000000000],[9620.740000000000000000,0.004000000000000000],[9620.680000000000000000,0.000469000000000000],[9620.450000000000000000,0.001846000000000000],[9620.400000000000000000,0.363700000000000000],[9620.360000000000000000,1.245843000000000000],[9620.330000000000000000,0.002977000000000000],[9620.130000000000000000,0.000146000000000000],[9620.050000000000000000,0.103835000000000000],[9620.000000000000000000,0.451638000000000000],[9619.900000000000000000,0.750000000000000000],[9619.780000000000000000,0.024200000000000000],[9619.750000000000000000,0.000301000000000000],[9619.680000000000000000,0.008821000000000000],[9619.420000000000000000,1.817700000000000000],[9619.260000000000000000,0.300000000000000000],[9618.250000000000000000,0.000900000000000000],[9618.230000000000000000,0.004936000000000000],[9617.470000000000000000,0.042600000000000000],[9617.340000000000000000,0.311442000000000000],[9617.000000000000000000,0.003100000000000000],[9616.950000000000000000,0.100756000000000000],[9616.660000000000000000,1.722300000000000000],[9616.320000000000000000,0.007578000000000000],[9616.270000000000000000,0.500000000000000000],[9616.050000000000000000,0.001300000000000000],[9616.030000000000000000,2.491686000000000000],[9616.000000000000000000,20.000000000000000000],[9615.860000000000000000,0.200000000000000000],[9615.780000000000000000,0.389451000000000000],[9615.710000000000000000,0.200000000000000000],[9615.660000000000000000,0.103805000000000000],[9615.380000000000000000,0.000700000000000000],[9614.860000000000000000,0.001100000000000000],[9614.260000000000000000,0.001400000000000000],[9614.190000000000000000,0.015581000000000000],[9613.970000000000000000,0.000600000000000000],[9613.920000000000000000,0.020000000000000000],[9613.580000000000000000,0.000300000000000000],[9613.250000000000000000,0.007000000000000000],[9613.190000000000000000,0.103805000000000000],[9612.240000000000000000,0.041429000000000000],[9612.200000000000000000,0.103805000000000000],[9612.160000000000000000,0.001700000000000000],[9611.860000000000000000,0.003000000000000000],[9611.710000000000000000,0.001528000000000000],[9611.610000000000000000,0.186500000000000000],[9611.560000000000000000,0.027400000000000000],[9611.110000000000000000,0.000200000000000000],[9610.890000000000000000,0.485100000000000000],[9610.610000000000000000,0.103805000000000000],[9610.590000000000000000,0.000300000000000000],[9610.460000000000000000,0.258905000000000000],[9610.350000000000000000,0.000400000000000000],[9610.200000000000000000,0.000138000000000000],[9610.160000000000000000,0.007517000000000000],[9610.000000000000000000,0.062175000000000000],[9609.900000000000000000,0.600000000000000000],[9609.810000000000000000,0.000400000000000000],[9609.790000000000000000,0.000360000000000000],[9609.230000000000000000,0.311415000000000000],[9609.200000000000000000,0.311415000000000000],[9609.090000000000000000,0.000300000000000000],[9608.880000000000000000,0.001500000000000000],[9608.460000000000000000,0.000200000000000000],[9608.410000000000000000,0.028200000000000000],[9608.390000000000000000,0.000156000000000000],[9608.160000000000000000,0.000200000000000000],[9608.010000000000000000,0.000200000000000000],[9607.680000000000000000,0.002600000000000000],[9607.010000000000000000,0.311442000000000000],[9606.900000000000000000,0.250000000000000000],[9606.370000000000000000,0.260000000000000000],[9606.340000000000000000,0.311415000000000000],[9606.080000000000000000,1.014900000000000000],[9605.700000000000000000,0.003000000000000000],[9605.600000000000000000,0.000974000000000000],[9605.000000000000000000,0.139777000000000000],[9604.670000000000000000,0.002948000000000000],[9604.080000000000000000,0.007578000000000000],[9604.070000000000000000,0.003000000000000000],[9603.810000000000000000,0.000600000000000000],[9603.560000000000000000,0.044718000000000000],[9603.440000000000000000,0.005500000000000000],[9603.000000000000000000,0.100000000000000000],[9602.880000000000000000,0.156218000000000000],[9602.870000000000000000,0.000200000000000000],[9602.800000000000000000,0.017476000000000000],[9602.790000000000000000,0.002300000000000000],[9602.700000000000000000,0.250000000000000000],[9602.660000000000000000,0.001616000000000000],[9602.600000000000000000,0.250000000000000000],[9602.000000000000000000,1.009258000000000000],[9601.730000000000000000,0.002200000000000000],[9601.540000000000000000,0.800000000000000000],[9601.530000000000000000,0.320000000000000000],[9601.400000000000000000,0.003200000000000000],[9601.330000000000000000,0.103805000000000000],[9601.310000000000000000,0.300000000000000000]],"asks":[[9634.490000000000000000,10.500000000000000000],[9634.500000000000000000,1.126100000000000000],[9634.510000000000000000,0.212200000000000000],[9634.540000000000000000,0.188584173927348893],[9634.610000000000000000,1.534200000000000000],[9634.690000000000000000,0.000138000000000000],[9635.000000000000000000,0.137611000000000000],[9635.830000000000000000,0.006000000000000000],[9636.300000000000000000,3.114944000000000000],[9636.390000000000000000,0.253000000000000000],[9636.400000000000000000,0.912000000000000000],[9636.800000000000000000,1.000000000000000000],[9636.830000000000000000,0.230000000000000000],[9637.320000000000000000,0.050800000000000000],[9637.330000000000000000,0.096759000000000000],[9638.500000000000000000,1.500000000000000000],[9638.780000000000000000,0.013800000000000000],[9639.000000000000000000,0.102768000000000000],[9639.600000000000000000,0.424600000000000000],[9640.000000000000000000,0.803365000000000000],[9640.370000000000000000,0.326738000000000000],[9640.640000000000000000,2.491956000000000000],[9640.810000000000000000,0.007578000000000000],[9640.830000000000000000,0.413500000000000000],[9640.890000000000000000,0.054163000000000000],[9640.970000000000000000,0.055226000000000000],[9641.360000000000000000,0.390372000000000000],[9641.450000000000000000,0.108325000000000000],[9641.480000000000000000,0.020000000000000000],[9641.720000000000000000,0.057113000000000000],[9641.820000000000000000,0.162488000000000000],[9641.990000000000000000,0.150000000000000000],[9642.000000000000000000,1.057970000000000000],[9642.280000000000000000,0.487464000000000000],[9642.470000000000000000,0.207610000000000000],[9642.490000000000000000,0.934167000000000000],[9642.560000000000000000,0.267558000000000000],[9643.310000000000000000,0.003000000000000000],[9643.350000000000000000,0.124500000000000000],[9643.850000000000000000,0.446471000000000000],[9643.890000000000000000,1.245556000000000000],[9644.000000000000000000,0.018782000000000000],[9644.030000000000000000,0.027335000000000000],[9644.400000000000000000,0.385300000000000000],[9644.490000000000000000,0.599003000000000000],[9645.000000000000000000,1.740523000000000000],[9645.430000000000000000,0.800000000000000000],[9646.000000000000000000,0.019536000000000000],[9646.710000000000000000,0.114000000000000000],[9646.930000000000000000,0.000138000000000000],[9647.000000000000000000,1.003562252250637074],[9647.350000000000000000,0.001400000000000000],[9647.490000000000000000,0.039705000000000000],[9647.510000000000000000,0.056000000000000000],[9647.580000000000000000,0.799017000000000000],[9647.640000000000000000,0.018791000000000000],[9647.740000000000000000,2.491112000000000000],[9647.870000000000000000,0.044950000000000000],[9648.000000000000000000,0.292688000000000000],[9648.290000000000000000,0.000400000000000000],[9648.700000000000000000,0.015200000000000000],[9649.100000000000000000,0.098240000000000000],[9649.190000000000000000,0.003000000000000000],[9649.250000000000000000,0.083000000000000000],[9649.380000000000000000,0.485100000000000000],[9650.000000000000000000,3.611456000000000000],[9650.030000000000000000,0.432198000000000000],[9650.580000000000000000,0.050118000000000000],[9650.920000000000000000,0.207610000000000000],[9651.270000000000000000,0.430000000000000000],[9651.340000000000000000,0.415219000000000000],[9652.010000000000000000,0.207628000000000000],[9652.980000000000000000,0.415220000000000000],[9653.060000000000000000,0.007578000000000000],[9653.520000000000000000,0.207628000000000000],[9653.640000000000000000,0.015578000000000000],[9654.200000000000000000,1.014900000000000000],[9654.400000000000000000,0.003000000000000000],[9655.000000000000000000,0.606954000000000000],[9655.050000000000000000,0.415256000000000000],[9655.740000000000000000,0.207610000000000000],[9656.010000000000000000,0.096422000000000000],[9656.370000000000000000,1.400000000000000000],[9657.140000000000000000,0.013500000000000000],[9657.250000000000000000,0.415219000000000000],[9657.480000000000000000,0.217780000000000000],[9657.540000000000000000,0.640000000000000000],[9658.000000000000000000,0.499000000000000000],[9659.180000000000000000,0.000138000000000000],[9659.230000000000000000,2.000000000000000000],[9660.000000000000000000,1.099306000000000000],[9660.490000000000000000,0.002300000000000000],[9660.700000000000000000,3.750000000000000000],[9661.370000000000000000,0.003000000000000000],[9661.730000000000000000,0.200000000000000000],[9662.330000000000000000,1.500000000000000000],[9662.620000000000000000,0.001996000000000000],[9662.700000000000000000,0.250000000000000000],[9663.260000000000000000,0.000188000000000000],[9663.540000000000000000,0.015556000000000000],[9663.630000000000000000,0.000300000000000000],[9663.880000000000000000,0.300360000000000000],[9664.110000000000000000,1.187000000000000000],[9664.120000000000000000,1.122300000000000000],[9665.000000000000000000,0.200000000000000000],[9665.270000000000000000,0.124300000000000000],[9665.300000000000000000,0.047578000000000000],[9665.660000000000000000,0.200000000000000000],[9665.900000000000000000,0.200000000000000000],[9666.000000000000000000,0.020000000000000000],[9666.240000000000000000,0.003100000000000000],[9666.640000000000000000,0.001400000000000000],[9666.660000000000000000,0.001289000000000000],[9667.220000000000000000,0.003000000000000000],[9667.430000000000000000,2.421304000000000000],[9667.580000000000000000,0.000400000000000000],[9668.000000000000000000,0.200000000000000000],[9668.400000000000000000,0.250000000000000000],[9668.980000000000000000,0.048750000000000000],[9669.000000000000000000,0.697143981694073845],[9670.000000000000000000,0.435564000000000000],[9670.040000000000000000,0.011700000000000000],[9670.090000000000000000,0.011072000000000000],[9670.360000000000000000,0.064599000000000000],[9670.400000000000000000,0.250000000000000000],[9671.350000000000000000,0.510000000000000000],[9671.420000000000000000,0.000138000000000000],[9671.710000000000000000,0.010400000000000000],[9673.000000000000000000,0.068864000000000000],[9673.460000000000000000,0.000200000000000000],[9673.830000000000000000,0.001300000000000000],[9673.960000000000000000,0.200000000000000000],[9674.990000000000000000,0.191993000000000000],[9675.000000000000000000,0.200000000000000000],[9675.120000000000000000,0.015562000000000000],[9676.100000000000000000,0.250000000000000000],[9676.770000000000000000,0.015535000000000000],[9676.900000000000000000,0.000400000000000000],[9677.120000000000000000,0.770000000000000000],[9677.270000000000000000,0.000300000000000000],[9677.470000000000000000,0.186300000000000000],[9677.550000000000000000,0.007578000000000000],[9677.770000000000000000,0.300000000000000000],[9678.100000000000000000,0.250000000000000000],[9679.000000000000000000,0.010000000000000000],[9679.180000000000000000,0.004200000000000000],[9679.500000000000000000,0.250000000000000000],[9679.810000000000000000,0.099800000000000000],[9680.000000000000000000,1.569130000000000000],[9680.170000000000000000,0.008060000000000000]]}}

        DepthRequest depthRequest = new DepthRequest();
        depthRequest.setSymbol("btcusdt");
        depthRequest.setType("step0");
        DepthResponse depth = client.depth(depthRequest);
        print(depth);

        //------------------------------------------------------ trade -------------------------------------------------------
        //{"status":"ok","ch":"market.ethusdt.trade.detail","ts":1564368035989,"err-code":null,"err-msg":null,"tick":{"id":101786818265,"ts":1564368035283,"data":[{"amount":9.7037,"ts":1564368035283,"id":10178681826542538309329,"price":213.64,"direction":"buy"}]}}
        TradeResponse trade = client.trade("ethusdt");
        print(trade);

        //------------------------------------------------------ historyTrade -------------------------------------------------------
        HistoryTradeResponse historyTrade = client.historyTrade("ethusdt", "20");
        print(historyTrade);

        //------------------------------------------------------ historyTrade -------------------------------------------------------
        DetailResponse detailTrade = client.detail("ethusdt");
        print(detailTrade);

        //------------------------------------------------------ symbols -------------------------------------------------------
        SymbolsResponse symbols = client.symbols("btcusdt");
        print(symbols);

        //------------------------------------------------------ Currencys -------------------------------------------------------
        CurrencysResponse currencys = client.currencys("btcusdt");
        print(currencys);

        //------------------------------------------------------ Currencys -------------------------------------------------------
        TimestampResponse timestamp = client.timestamp();
        print(timestamp);

        //------------------------------------------------------ accounts -------------------------------------------------------
        AccountsResponse accounts = client.accounts();
        print(accounts);

        //------------------------------------------------------ balance -------------------------------------------------------
        List<Accounts> list = (List<Accounts>) accounts.getData();
        BalanceResponse balance = client.balance(String.valueOf(list.get(0).getId()));
        BalanceResponse balance2 = client.balance(String.valueOf(list.get(1).getId()));

        print(balance); //spot
        print(balance2);//otc

        Long orderId = 123L;
        if (!list.isEmpty()) {
            // find account id:
            Accounts account = list.get(0);
            long accountId = account.getId();
            // create order:
            CreateOrderRequest createOrderReq = new CreateOrderRequest();
            createOrderReq.accountId = String.valueOf(accountId);
            createOrderReq.amount = "0.02";
            createOrderReq.price = "0.1";
            createOrderReq.symbol = "eosusdt";
            createOrderReq.type = CreateOrderRequest.OrderType.BUY_LIMIT;
            createOrderReq.source = "api";

            //------------------------------------------------------ 创建订单  -------------------------------------------------------
            orderId = client.createOrder(createOrderReq);
            print(orderId);
            // place order:

            //------------------------------------------------------ 执行订单  -------------------------------------------------------
            String r = client.placeOrder(orderId);
            print(r);
        }

        //------------------------------------------------------ submitcancel 取消订单 -------------------------------------------------------

//    SubmitcancelResponse submitcancel = client.submitcancel(orderId.toString());
//    print(submitcancel);

        //------------------------------------------------------ submitcancel 批量取消订单-------------------------------------------------------
//    String[] orderList = {"727554767","727554766",""};
//    String[] orderList = {String.valueOf(orderId)};
        List orderList = new ArrayList();
        orderList.add(orderId);
        BatchcancelResponse submitcancels = client.submitcancels(orderList);
        print(submitcancels);

        //------------------------------------------------------ ordersDetail 订单详情 -------------------------------------------------------
        OrdersDetailResponse ordersDetail = client.ordersDetail(String.valueOf(orderId));
        print(ordersDetail);

        //------------------------------------------------------ ordersDetail 已经成交的订单详情 -------------------------------------------------------
//    String.valueOf(orderId)
        MatchresultsOrdersDetailResponse matchresults = client.matchresults("714746923");
        print(ordersDetail);

        //------------------------------------------------------ ordersDetail 已经成交的订单详情 -------------------------------------------------------
//    String.valueOf(orderId)
        IntrustOrdersDetailRequest req = new IntrustOrdersDetailRequest();
        req.symbol = "btcusdt";
        req.types = IntrustOrdersDetailRequest.OrderType.BUY_LIMIT;
//    req.startDate = "2018-01-01";
//    req.endDate = "2018-01-14";
        req.states = IntrustOrdersDetailRequest.OrderStates.FILLED;
//    req.from = "";
//    req.direct = "";
//    req.size = "";


//    public String symbol;	   //true	string	交易对		btcusdt, bccbtc, rcneth ...
//    public String types;	   //false	string	查询的订单类型组合，使用','分割		buy-market：市价买, sell-market：市价卖, buy-limit：限价买, sell-limit：限价卖
//    public String startDate;   //false	string	查询开始日期, 日期格式yyyy-mm-dd
//    public String endDate;	   //false	string	查询结束日期, 日期格式yyyy-mm-dd
//    public String states;	   //true	string	查询的订单状态组合，使用','分割		pre-submitted 准备提交, submitted 已提交, partial-filled 部分成交,
//    // partial-canceled 部分成交撤销, filled 完全成交, canceled 已撤销
//    public String from;	       //false	string	查询起始 ID
//    public String direct;	   //false	string	查询方向		prev 向前，next 向后
//    public String size;	       //false	string	查询记录大小


        //------------------------------------------------------ order 查询当前委托、历史委托 -------------------------------------------------------

        IntrustDetailResponse intrustDetail = client.intrustOrdersDetail(req);
        print(intrustDetail);


//    // get accounts:
//    List<Account> accounts1 = client.getAccounts();
//    print(accounts1);

    }

    static void print(Object obj) {
        try {
            System.out.println(JsonUtil.writeValue(obj));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
